package com.ngc.mall.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ngc.mall.common.configure.exception.BusinessException;
import com.ngc.mall.common.configure.mybatisPlus.BaseServiceImpl;
import com.ngc.mall.common.configure.response.ResponseCode;
import com.ngc.mall.domain.AttachmentDomain;
import com.ngc.mall.domain.SaleAdDomain;
import com.ngc.mall.dto.AttachmentDTO;
import com.ngc.mall.dto.FileInfoDTO;
import com.ngc.mall.dto.SaleAdDTO;
import com.ngc.mall.mapper.SaleAdMapper;
import com.ngc.mall.service.IAttachmentService;
import com.ngc.mall.service.ICosService;
import com.ngc.mall.service.ISaleAdService;
import com.ngc.mall.vo.AttachmentVO;
import com.ngc.mall.vo.SaleAdVO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class SaleAdServiceImpl extends BaseServiceImpl<SaleAdMapper, SaleAdDomain> implements ISaleAdService {
    @Resource
    private IAttachmentService iAttachmentService;
    @Resource
    private ICosService iCosService;
    @Override
    public List<SaleAdVO> selectAll(){
       List<SaleAdDomain> saleAdDomainList=super.list();
       List<SaleAdVO> saleAdVOList = saleAdDomainList.stream().map(saleAdDomain -> {
           return SaleAdVO
                   .builder()
                   .name(saleAdDomain.getName())
                   .note(saleAdDomain.getNote())
                   .img(saleAdDomain.getImg())
                   .isEnabled(saleAdDomain.getIsEnabled())
                   .expireTime(saleAdDomain.getExpireTime())
                   .sort(saleAdDomain.getSort())
                   .id(saleAdDomain.getId())
                   .createTime(saleAdDomain.getCreateTime())
                   .build();
       }).collect(Collectors.toList());

       return saleAdVOList;
    };

    @Override
    public Long createSaleAd(SaleAdDTO dto){
        SaleAdDomain domain=SaleAdDomain
                .builder()
                .name(dto.getName())
                .sort(dto.getSort())
                .note(dto.getNote())
                .expireTime(dto.getExpireTime())
                .isEnabled(dto.getIsEnabled())
                .img(dto.getImg())
                .build();
        Long id=super.create(domain);
        return id;
    }

    public AttachmentDomain getAttachmentInfo(AttachmentDTO attachmentDTO,Long id){
        FileInfoDTO fileInfoDTO=iCosService.getFormalObject(attachmentDTO.getFileName());
        AttachmentDomain attachmentDomain=AttachmentDomain
                .builder()
                .recordId(id)
                .sort(attachmentDTO.getSort())
                .fileSize(fileInfoDTO.getFileSize())
                .filePath(fileInfoDTO.getFileUrl())
                .type(7)
                .fileType(getFileType(attachmentDTO.getFileName()))
                .build();
        return attachmentDomain;
    }

    public String getFileType(String fileName){
        String fileType=fileName.substring(fileName.lastIndexOf('.'));
        return fileType;
    }

    @Override
    public Boolean updateAdStatus(Boolean status,Long id){
        return super.update(Wrappers.<SaleAdDomain>lambdaUpdate().eq(SaleAdDomain::getId,id).set(SaleAdDomain::getIsEnabled,status));
    }

    @Override
    public Boolean updateById(SaleAdDTO dto,Long id){
        SaleAdDomain domain=SaleAdDomain
                .builder()
                .name(dto.getName())
                .sort(dto.getSort())
                .note(dto.getNote())
                .expireTime(dto.getExpireTime())
                .isEnabled(dto.getIsEnabled())
                .img(dto.getImg())
                .build();
        super.update(domain,Wrappers.<SaleAdDomain>lambdaUpdate().eq(SaleAdDomain::getId,id));
        return true;
    }
    @Override
    public IPage<SaleAdDomain> getPageList(int current, int size){
        Page<SaleAdDomain> page=new Page<>(current,size);
        IPage<SaleAdDomain> pages=super.page(page);
        return pages;
    }
    @Override
    public SaleAdVO getAdById(Long id){
        AttachmentDomain attachmentDomain=iAttachmentService.getOne(Wrappers.<AttachmentDomain>lambdaQuery().eq(AttachmentDomain::getRecordId,id));
        SaleAdDomain saleAdDomain= super.getById(id);
        SaleAdVO vo=SaleAdVO
                .builder()
                .img(saleAdDomain.getImg())
                .createTime(saleAdDomain.getCreateTime())
                .id(saleAdDomain.getId())
                .name(saleAdDomain.getName())
                .note(saleAdDomain.getNote())
                .expireTime(saleAdDomain.getExpireTime())
                .sort(saleAdDomain.getSort())
                .isEnabled(saleAdDomain.getIsEnabled())
                .build();
        return vo;
    }


}
